﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using PTCapture.UserControls;

namespace PTCapture.Forms
{
    public partial class MainForm : Form
    {
        #region Controls

        /// <summary>
        /// Selected index changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbPlayback_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPlayback1.SelectedIndex > -1 ||
                cbPlayback2.SelectedIndex > -1)
            {
                btnPlay.Enabled = true;
            }
            else
            {
                btnPlay.Enabled = false;
            }
        }

        #endregion

        #region PlaybackButtons

        /// <summary>
        /// Step back one frame.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRewindStep_Click(object sender, EventArgs e)
        {
            // The WMVs we are recording are not capable of stepping back.
            // So change by average rate
            double rate = 0;
            double position = 0;

            if (cbPlayback1.SelectedIndex > -1)
            {
                // Change 1
                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).BasicVideo.get_AvgTimePerFrame(out rate);
                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).MediaPosition.get_CurrentPosition(out position);

                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).MediaPosition.put_CurrentPosition(position - rate);
                moviePlayerUIControl1.UpdatePositionData(true);
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                // Change 2
                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl2"]).BasicVideo.get_AvgTimePerFrame(out rate);
                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl2"]).MediaPosition.get_CurrentPosition(out position);

                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl2"]).MediaPosition.put_CurrentPosition(position - rate);
                moviePlayerUIControl2.UpdatePositionData(true);
            }

            tss1.Text = "Paused.";
        }

        /// <summary>
        /// Step forward one frame.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFastForwardStep_Click(object sender, EventArgs e)
        {
            if (cbPlayback1.SelectedIndex > -1)
            {
                // Change 1
                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).StepFrames(1);
                moviePlayerUIControl1.UpdatePositionData(true);
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                // Change 2
                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl2"]).StepFrames(1);
                moviePlayerUIControl2.UpdatePositionData(true);
            }

            tss1.Text = "Paused.";
        }

        /// <summary>
        /// Play videos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlay_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = false;
            btnStop.Enabled = true;
            btnPause.Enabled = true;
            btnRewind.Enabled = true;
            btnRewindStep.Enabled = false;
            //btnRewindTo.Enabled = true;
            btnFastForward.Enabled = true;
            btnFastForwardStep.Enabled = false;
            //btnFastForwardTo.Enabled = true;

            if (cbPlayback1.SelectedIndex > -1)
            {
                PlayMovie(moviePlayerUIControl1, ((ListItem)cbPlayback1.SelectedItem).Value);
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                PlayMovie(moviePlayerUIControl2, ((ListItem)cbPlayback2.SelectedItem).Value);
            }

            // Reset both to time 0 so they are in sync, as starting play can take a minute
            if (cbPlayback1.SelectedIndex > -1)
            {
                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).MediaPosition.put_CurrentPosition(0);
            }
            if (cbPlayback2.SelectedIndex > -1)
            {
                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl1"]).MediaPosition.put_CurrentPosition(0);
            }

            moviePlayerUIControl1.StartTimer();
            moviePlayerUIControl2.StartTimer();
            
            tss1.Text = string.Format("Playing at {0}x", ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).GetRate());
        }

        /// <summary>
        /// Pause videos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = false;
            btnStop.Enabled = true;
            btnPause.Enabled = true;
            btnRewind.Enabled = true;
            btnRewindStep.Enabled = true;
            //btnRewindTo.Enabled = true;
            btnFastForward.Enabled = true;
            btnFastForwardStep.Enabled = true;
            //btnFastForwardTo.Enabled = true;

            if (cbPlayback1.SelectedIndex > -1)
            {
                moviePlayerUIControl1.Controls["btnSnapshot"].Enabled = true;

                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).PauseClip();
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                moviePlayerUIControl2.Controls["btnSnapshot"].Enabled = true;

                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl1"]).PauseClip();
            }

            tss1.Text = "Paused.";
        }

        /// <summary>
        /// Stop videos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = true;
            btnStop.Enabled = false;
            btnPause.Enabled = false;
            btnRewind.Enabled = false;
            btnRewindStep.Enabled = false;
            //btnRewindTo.Enabled = false;
            btnFastForward.Enabled = false;
            btnFastForwardStep.Enabled = false;
            //btnFastForwardTo.Enabled = false;

            if (cbPlayback1.SelectedIndex > -1)
            {
                moviePlayerUIControl1.Controls["btnSnapshot"].Enabled = false;

                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).StopClip();
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                moviePlayerUIControl2.Controls["btnSnapshot"].Enabled = false;

                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl1"]).StopClip();
            }

            tss1.Text = "Stopped.";
        }

        /// <summary>
        /// Change playback rate backwards
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRewind_Click(object sender, EventArgs e)
        {
            // May not work for our wmvs. We can just have a variable and put_position if we need to.
            if (cbPlayback1.SelectedIndex > -1)
            {
                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).ModifyRate(-0.25f);
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl1"]).ModifyRate(-0.25f);
            }

            tss1.Text = string.Format("Playing at {0}x", ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).GetRate());
        }

        /// <summary>
        /// Change playback rate forwards.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFastForward_Click(object sender, EventArgs e)
        {
            if (cbPlayback1.SelectedIndex > -1)
            {
                // May not work for our wmvs. We can just have a variable and put_position if we need to.
                ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).ModifyRate(0.25f);
            }

            if (cbPlayback2.SelectedIndex > -1)
            {
                // May not work for our wmvs. We can just have a variable and put_position if we need to.
                ((MoviePlayerControl)moviePlayerUIControl2.Controls["moviePlayerControl1"]).ModifyRate(0.25f);
            }

            tss1.Text = string.Format("Playing at {0}x", ((MoviePlayerControl)moviePlayerUIControl1.Controls["moviePlayerControl1"]).GetRate());
        }

        #endregion

        #region Support

        /// <summary>
        /// Play a movie
        /// </summary>
        /// <param name="moviePlayerUIControl1"></param>
        private void PlayMovie(MoviePlayerUIControl player, string fileName)
        {
            float max = 0, min = 0, current = 0;
            player.Controls["btnSnapshot"].Enabled = true;

            ((MoviePlayerControl)player.Controls["moviePlayerControl1"]).PlayClip(fileName);

            ((MoviePlayerControl)player.Controls["moviePlayerControl1"]).GetRanges(DirectShowLib.VMR9ProcAmpControlFlags.Brightness, ref min, ref max, ref current);
            player.brightnessMax = max;
            player.brightnessMin = min;

            // Trackbars are constrained from 0..100, so just convert to value from percent
            ((TrackBar)player.Controls["tbBrightness"]).Value = (int)((current - min) / (max - min) * 100);

            ((MoviePlayerControl)player.Controls["moviePlayerControl1"]).GetRanges(DirectShowLib.VMR9ProcAmpControlFlags.Contrast, ref min, ref max, ref current);
            player.contrastMax = max;
            player.contrastMin = min;

            // Trackbars are constrained from 0..100, so just convert to value from percent
            ((TrackBar)player.Controls["tbContrast"]).Value = (int)((current - min) / (max - min) * 100);
        }

        #endregion

        #region DelegateCallbacks

        /// <summary>
        /// Reassign images when a snapshot is taken.
        /// </summary>
        /// <param name="imageName"></param>
        /// <returns></returns>
        bool moviePlayerUIControl1_AssignImages(string imageName)
        {
            PopulateImages();

            return true;
        }

        #endregion
    }
}
